package com.lambkit.cms.start;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.handler.Handler;
import com.jfinal.kit.StrKit;

public class MyFakeStaticHandler extends Handler {

	private String viewPostfix;
	private String viewPostfixIndex;
	
	public MyFakeStaticHandler() {
		viewPostfix = ".html";
		viewPostfixIndex = "index.html";
	}
	
	public MyFakeStaticHandler(String viewPostfix) {
		if (StrKit.isBlank(viewPostfix)) {
			throw new IllegalArgumentException("viewPostfix can not be blank.");
		}
		this.viewPostfix = viewPostfix;
		this.viewPostfixIndex = "index" + viewPostfix;
	}
	
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		if ("/".equals(target)) {
			next.handle(target, request, response, isHandled);
			return;
		}
		int index = target.lastIndexOf(viewPostfixIndex);
		if (index != -1) {
			target = target.substring(0, index);
		} else  {
			int fixindex = target.lastIndexOf(viewPostfix);
			if (fixindex != -1) {
				target = target.substring(0, fixindex);
			}
		}
		next.handle(target, request, response, isHandled);
	}
}
